/*
XPTracker - Alternative Agile Tracking Tool
Copyright (C) 2006-2009  Stephen Starkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
package org.xptracker.web;

import net.sourceforge.stripes.action.*;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.security.action.Secure;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import org.xptracker.domain.TimeSegment;
import org.xptracker.domain.Timeline;
import org.xptracker.persistence.PersistenceManager;

public class TimeSegmentsActionBean extends StandardActionBean {
  private Timeline timeline;

  @ValidateNestedProperties({
      @Validate(field="name", required=true),
      @Validate(field="startDate", required=true),
      @Validate(field="stopDate", required=true)})
  private TimeSegment segment;

  public Timeline getTimeline() { return timeline; }
  public void setTimeline(Timeline timeline) { this.timeline = timeline; }

  public TimeSegment getSegment() { return segment; }
  public void setSegment(TimeSegment segment) { this.segment = segment; }

  @DefaultHandler
  @DontValidate
  public Resolution listSegments() {
    return new ForwardResolution("/timeSegments.jsp");
  }

  @HandlesEvent("Add")
  @Secure(roles="admin")
  public Resolution addSegment() {
    return persistenceManager.inTransaction(new PersistenceManager.UpdateHandler<Resolution>() {
      public Resolution doUpdate(PersistenceManager.UpdateReceiver receiver) {
        receiver.insert(segment);
        Timeline timeline = loadTimeline();
        timeline.getSegments().add(segment);
        receiver.update(timeline);
        addMessage("Added segment: {0}", segment);
        return new RedirectResolution(TimeSegmentsActionBean.class).flash(TimeSegmentsActionBean.this).
            addParameter("timelineId", timeline.getId());
      }
    });
  }

  @HandlesEvent("Edit")
  @Secure(roles="admin")
  public Resolution editSegment() {
    return persistenceManager.inTransaction(new PersistenceManager.UpdateHandler<Resolution>() {
      public Resolution doUpdate(PersistenceManager.UpdateReceiver receiver) {
        TimeSegment localSegment = domain.loadEntity(TimeSegment.class, segment.getId());
        localSegment.setName(segment.getName());
        localSegment.setStartDate(segment.getStartDate());
        localSegment.setStopDate(segment.getStopDate());
        receiver.update(localSegment);
        addMessage("Saved segment: {0}", localSegment);
        return new RedirectResolution(TimeSegmentsActionBean.class).flash(TimeSegmentsActionBean.this).
            addParameter("timelineId", timeline.getId());
      }
    });
  }

  @HandlesEvent("Delete")
  @Secure(roles="admin")
  @DontValidate
  public Resolution deleteSegment() {
    return persistenceManager.inTransaction(new PersistenceManager.UpdateHandler<Resolution>() {
      public Resolution doUpdate(PersistenceManager.UpdateReceiver receiver) {
        receiver.insert(segment);
        Timeline timeline = loadTimeline();
        TimeSegment segment = loadSegment();
        if (timeline.getSegments().remove(segment)) {
          receiver.remove(segment);
          addMessage("Removed segment: ''{0}''", segment);
          receiver.update(timeline);
        } else {
          addGlobalError("Timeline did not contain segment ''{1}''", segment.getName());
        }
        return new RedirectResolution(TimeSegmentsActionBean.class).flash(TimeSegmentsActionBean.this).
            addParameter("timelineId", timeline.getId());
      }
    });
  }

  private TimeSegment loadSegment() {
    return domain.loadEntity(TimeSegment.class, param("segmentId"));
  }

  @Before(stages=LifecycleStage.BindingAndValidation)
  public void provideTimeline() {
    timeline = loadTimeline();
    segment = new TimeSegment();
  }

  private Timeline loadTimeline() {
    return domain.loadEntity(Timeline.class, param("timelineId"));
  }
}
